//
//  ChangePasswordViewController.m
//  ShopProject
//
//  Created by 小焘无量 on 2018/9/27.
//  Copyright © 2018年 ShopProject. All rights reserved.
//

#import "ChangePasswordViewController.h"

@interface ChangePasswordViewController ()

@property (weak, nonatomic) IBOutlet UITextField *oldPasswordTF;
@property (weak, nonatomic) IBOutlet UITextField *changeFirstTF;
@property (weak, nonatomic) IBOutlet UITextField *changeSecondTF;
@property (weak, nonatomic) IBOutlet UIButton *sureChangeBtn;


@end

@implementation ChangePasswordViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self UIConfig];
}

- (void)UIConfig {
    self.title = @"修改密码";
    
    _sureChangeBtn.backgroundColor = MainColor;
}


- (IBAction)changePasswordBtnPress:(id)sender {
    
    [self.view endEditing:YES];
    
    if (![self changeDateCheck]) {
        return;
    }
    
    JQFMDB *dataBase = [JQFMDB shareDatabase];
    NSString *loginTel = [UserDefaults readFromPlist:LOGIN_STATUS_TEL];
    OO_UserInfoModel *model = [OO_UserInfoModel new];
    model.password = _changeSecondTF.text;
    
    NSMutableDictionary *dic = [NSMutableDictionary new];
    dic[@"oldPassword"] = _oldPasswordTF.text;
    dic[@"newPassword"] = _changeSecondTF.text;
    
    // 检测旧密码
    if (![model.password isEqualToString:_oldPasswordTF.text]) {
        dic[@"type"] = @"0";
        [PPNetworkHelper POST:[NSString stringWithFormat:@"%@%@",HTTP,CHANGEPASSWORD_URL] parameters:dic success:^(id responseObject) {
            [MBProgressHUD showTipMessageInView:@"旧密码不正确"];
        } failure:^(NSError *error) {
            [MBProgressHUD showTipMessageInView:@"网络请求失败"];
        }];
    }else {
        if ([dataBase jq_updateTable:USERINFOTABLE dicOrModel:model whereFormat:@"where phone = %@",loginTel]) {
            dic[@"type"] = @"1";
            [PPNetworkHelper POST:[NSString stringWithFormat:@"%@%@",HTTP,CHANGEPASSWORD_URL] parameters:dic success:^(id responseObject) {
                [self.navigationController popViewControllerAnimated:YES];
                [MBProgressHUD showTipMessageInView:@"修改成功"];
            } failure:^(NSError *error) {
                [MBProgressHUD showTipMessageInView:@"网络请求失败"];
            }];
        }else {
            [MBProgressHUD showTipMessageInView:@"未知错误"];
        }
    }
}

-(BOOL)changeDateCheck {
    if ([CommonAPI StringIsNull:_oldPasswordTF.text]) {
        [MBProgressHUD showTipMessageInView:@"请输入旧密码"];
        return NO;
    }
    
    if ([CommonAPI StringIsNull:_changeFirstTF.text]) {
        [MBProgressHUD showTipMessageInView:@"请输入密码"];
        return NO;
    }
    if (![CommonAPI passwordCheck:_changeFirstTF.text]) {
        [MBProgressHUD showTipMessageInView:@"密码请保持在6-16位"];
        return NO;
    }
    if (![_changeFirstTF.text isEqualToString:_changeSecondTF.text]) {
        [MBProgressHUD showTipMessageInView:@"两次密码请保持一致"];
        return NO;
    }
    
    return YES;
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
